package cn.tedu.bs.admin.service.impl;

import cn.tedu.bs.admin.ex.ServiceException;
import cn.tedu.bs.admin.mapper.ProjectMonthlyMapper;
import cn.tedu.bs.admin.pojo.entity.ProjectMonthly;
import cn.tedu.bs.admin.pojo.param.ProjectMonthlyAddNewParam;
import cn.tedu.bs.admin.pojo.param.ProjectMonthlyUpdateInfoParam;
import cn.tedu.bs.admin.pojo.vo.ProjectMonthlyListItemVO;
import cn.tedu.bs.admin.pojo.vo.ProjectMonthlyStandardVO;
import cn.tedu.bs.admin.pojo.vo.PageData;
import cn.tedu.bs.admin.service.IProjectMonthlyService;
import cn.tedu.bs.admin.util.PageInfoToPageDataConverter;
import cn.tedu.bs.admin.web.ServiceCode;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

import static cn.tedu.bs.admin.util.ServiceUtil.saveFileToStorage;

@Slf4j
@Service
public class ProjectMonthlyImpl implements IProjectMonthlyService {
    @Autowired
    private ProjectMonthlyMapper projectMonthlyMapper;

    @Override
    public void addNew(ProjectMonthlyAddNewParam projectMonthlyAddNewParam, MultipartFile file) {
        log.debug("开始处理【添加项目月统计】的业务，参数：{}", projectMonthlyAddNewParam);
// 将项目月统计数据写入到数据库中
        ProjectMonthly projectMonthly = new ProjectMonthly();
        BeanUtils.copyProperties(projectMonthlyAddNewParam, projectMonthly);
        projectMonthly.setGmtCreated(LocalDateTime.now());
//        ProjectMonthly.setTime(LocalDateTime.now());
        //上传文件保存
        String url = "/null.png";
        if (file != null) {
            try {
                url = saveFileToStorage(file);
            } catch (IOException e) {
                String message = "添加图片失败，服务器忙，请稍后再试！[开发看见后，检查node图片服务器]";
                log.debug(message);
                throw new ServiceException(ServiceCode.ERR_INSERT, message);
            }
        }
        projectMonthly.setProjectPicture(url);
        int rows = projectMonthlyMapper.insert(projectMonthly);
        if (rows != 1) {
            String message = "添加项目月统计失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.debug("将新的项目月统计数据插入到数据库，完成！");
    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理【删除美容项目月统计】的业务，id：{}", id);
        projectMonthlyMapper.deleteById(id);
        log.debug("将id为{}的美容项目月统计数据从数据库中删除，完成！", id);
    }

    @Override
    public void updateInfoById(Long id, ProjectMonthlyUpdateInfoParam projectMonthlyUpdateInfoParam, MultipartFile file) {
        log.debug("开始处理【修改美容项目月统计详情】的业务，参数：{}", projectMonthlyUpdateInfoParam);
        // 检查项目月统计是否存在，如果不存在，则抛出异常
        QueryWrapper<ProjectMonthly> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        int countById = projectMonthlyMapper.selectCount(queryWrapper);
        log.debug("根据项目月统计ID统计匹配的项目月统计数量，结果：{}", countById);
        if (countById == 0) {
            String message = "修改项目月统计详情失败，项目月统计数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        //修改项目月统计
        ProjectMonthly projectMonthly = new ProjectMonthly();
        BeanUtils.copyProperties(projectMonthlyUpdateInfoParam, projectMonthly);
        projectMonthly.setId(id);
        String url = "/null.png";
        if (file != null) {
            try {
                url = saveFileToStorage(file);
            } catch (IOException e) {
                String message = "修改图片失败，服务器忙，请稍后再试！[开发看见后，检查node图片服务器]";
                log.debug(message);
                throw new ServiceException(ServiceCode.ERR_INSERT, message);
            }
        }
        log.debug(url);
        projectMonthly.setProjectPicture(url);
        int rows = projectMonthlyMapper.updateById(projectMonthly);
        if (rows != 1) {
            String message = "修改项目月统计失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
        log.debug("将新的美容项目月统计数据更新入到数据库，完成！");
    }

    @Override
    public ProjectMonthlyStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询项目月统计详情】的业务，参数：{}", id);
        ProjectMonthlyStandardVO queryResult = projectMonthlyMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "查询项目月统计详情失败，项目月统计数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public PageData<ProjectMonthlyListItemVO> list(Integer pageNum) {
        Integer pageSize = 5;
        return list(pageNum, pageSize);
    }

    @Override
    public PageData<ProjectMonthlyListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询项目月统计列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<ProjectMonthlyListItemVO> list = projectMonthlyMapper.list();
        PageInfo<ProjectMonthlyListItemVO> pageInfo = new PageInfo<>(list);
        PageData<ProjectMonthlyListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        log.debug("查询完成，即将返回：{}", pageData);
        return pageData;
    }

    //模糊查询项目名
    @Override
    public PageData<ProjectMonthlyListItemVO> getListByProjectName(Integer pageNum, Integer pageSize, String projectName) {
        log.debug("开始处理【根据名称查询项目月统计】的业务，参数：{}", projectName);
        PageHelper.startPage(pageNum, pageSize);
        List<ProjectMonthlyListItemVO> queryResult = projectMonthlyMapper.getListByProjectName(projectName);
        PageInfo<ProjectMonthlyListItemVO> pageInfo = new PageInfo<>(queryResult);
        PageData<ProjectMonthlyListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        log.debug("查询完成，即将返回：{}", queryResult);
        return pageData;
    }


}